package cn.com.netedge.iot.frontend.datasave.task;

import cn.com.netedge.iot.common.constants.CacheKeyConstants;
import cn.com.netedge.iot.common.utils.DateUtil;
import cn.com.netedge.iot.common.utils.RedisUtil;
import cn.com.netedge.iot.common.utils.StringUtil;
import cn.com.netedge.iot.frontend.datasave.config.DataSaveConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class HeartBeatTask {
    private static Logger logger = LoggerFactory.getLogger(HeartBeatTask.class);

    private static final int HEART_BEAT_INTERVAL = 30 * 1000; // 30 秒

    @Scheduled(fixedDelay = HEART_BEAT_INTERVAL)
    public void hearBeat() {
        String machineName = DataSaveConfig.getMachineName();
        if (StringUtil.isEmpty(machineName)) {
            logger.info("machine name is empty.");
            return;
        }
        Date now = new Date();
        RedisUtil.hset(CacheKeyConstants.KEY_FRONTEND_DATA_SAVE_MACHINE + "machine_map", machineName, DateUtil.DateToLongStr(now));
    }
}
